package com.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.sampler.getSize;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	public class BitmapAnimationAssets extends Sprite
	{
		private var _sourceList:Vector.<String>;
		private var _bitmapList:Vector.<BitmapData>;
		
		private var _bitmap:Bitmap;
		private var _loader:Loader;
		private var _path:String;//路径
		private var _suffixTag:String;//扩展名
		private var _imageName:String;//名字
		private var _prefixTag:String;//前缀
		private var _start:int;//开始
		private var _end:int;//结束
		
		private var _stopFrame:int = -1;
		
		private var _index:int;//当前调用数组的索引位置
		private var _interval:int;//循环变量
		/**帧频*/
		private var _dealy:int= 60;
		
		private var _loadOverCallBack:Function;//文件全部加载完成后执行的回调函数
		private var _currentFrame:int;//当前帧索引
		private var _invokingFunction:String;//掉用者,报错时可以使用
		/**
		 * @param startIndex 开始
		 * @param endIndex 结束
		 * @param path 路径
		 * @param prefixTag 序列图片共有的特征
		 * @param suffixTag 图片后缀
		 */		
		public function BitmapAnimationAssets(startIndex:int=-1,endIndex:int=-1,path:String = '',prefixTag:String = '',suffixTag:String = '.png',
											  loadOverCallBack:Function = null,stopFrame:int = -1){
			_start = startIndex;
			_end = endIndex;
			_path = path;
			_suffixTag = suffixTag;
			_prefixTag = prefixTag;
			_loadOverCallBack = loadOverCallBack;
			_stopFrame = stopFrame;
			
			init();
		}
		
		/**初始化数据*/		
		private function init():void{
			_sourceList = new Vector.<String>();
			_bitmapList = new Vector.<BitmapData>();
			_bitmap = new Bitmap();
			this.addChild(_bitmap);
			
			if(_start > _end) throw Error('索引错误,开始索引不能大于结束索引!');
			//倒叙添加,下面加载文件时,就是按照正确的索引,就不用在按照从小到大排序了
			for (var i:int = _end; i >= _start; i--) 
			{
				_sourceList.push(_prefixTag + i);
			}
			trace('导入【' + _sourceList.length + '】张图片【'+_sourceList.toString()+'】')
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
			loopLoad();
		}
		
		/**循环加载*/	
		private function loopLoad():void{
			_imageName = _sourceList.pop();//从尾部截取，尾部值总是比前一项值小
			_loader.load(new URLRequest(_path + _imageName + _suffixTag));
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
		}
		
		/**单次加载完毕执行*/		
		private function onCompleteHandler(e:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onCompleteHandler);
			_bitmapList.push((_loader.content as Bitmap).bitmapData);
			if(_sourceList.length > 0){
				loopLoad();
			}else{
				trace('序列图加载完毕!');
				_interval = setInterval(changeBitmapData,_dealy);
				
				setProperty();
				clearLoader();
			}
		}
		
		private function setProperty():void
		{
			if(_stopFrame >= 1) this.gotoAndStop(_stopFrame);
			
			if(_loadOverCallBack is Function) _loadOverCallBack();
			
		}
		
		/**清理loader*/		
		private function clearLoader():void{
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onCompleteHandler);
			_loader.unloadAndStop();
			_loader = null;
		}
		
		private function onErrorHandler(e:IOErrorEvent):void
		{
			trace('加载错误:' +　tracePath);
		}		
		
		/**打印文件路径*/		
		private function get tracePath():String{
			return _path + _imageName + _suffixTag;
		}
		
		/**替换_bitmap的bitmapData对象,实现动画效果*/		
		private function changeBitmapData():void{
			_currentFrame = _index;
			try{
				_bitmap.bitmapData = _bitmapList[_index];//替换bitmapData对象
			}catch(e:Error){
				throw Error(_invokingFunction);
			}
			
			initXY();
			this.width = _bitmap.width;
			this.height = _bitmap.height;
			
			setProperty();
			
			++_index;
			if(_index >= _bitmapList.length) _index = 0;
		}
		
		private function initXY():void{
			_bitmap.x = -_bitmap.width/2;
			_bitmap.y = -_bitmap.height;
		}
		
		/**检查索引是否合法*/		
		private function checkIndex():void{
			if(_index >= _bitmapList.length) _index = _bitmapList.length-1;
			if(_index < 0) _index = 0;
		}
		
		/**停止*/		
		public function stop():void{
			clearInterval(_interval);
		}
		
		/**下一帧*/		
		public function nextFrame():void{
			++_index;
			checkIndex();
			_bitmap.bitmapData = _bitmapList[_index];
			_currentFrame = _index;
			initXY();
		}
		
		/**前一帧*/	
		public function prevFrame():void{
			--_index;
			checkIndex();
			_bitmap.bitmapData = _bitmapList[_index];//替换bitmapData对象
			_currentFrame = _index;
			initXY();
		}
		
		/**停止在指定帧,起始为1*/	
		public function gotoAndStop(value:int):void{
			clearInterval(_interval);
			_index = value-1;
			checkIndex();
			_bitmap.bitmapData = _bitmapList[_index];//替换bitmapData对象
			_currentFrame = _index;
			initXY();
		}
		
		/**从指定帧播放,起始为1*/	
		public function gotoAndPlay(value:int):void{
			clearInterval(_interval);
			value - 1 >= _bitmapList.length ? _index = _bitmapList.length-1 : _index = value - 1;
			checkIndex();
			play();
		}
		
		/**播放*/		
		public function play():void{
			if(_sourceList.length > 0) throw Error('数据尚未加载完成不能播放!');
			clearInterval(_interval);
			trace('播放！')
			_interval = setInterval(changeBitmapData,_dealy);
		}
		
		/**设置循环时间*/		
		public function set intervalDealy(value:int):void{
			_dealy = value;
			clearInterval(_interval);
			trace('设置循环时间！')
			_interval = setInterval(changeBitmapData,_dealy);
		}
		
		/**当前帧*/		
		public function get currentFrame():int{
			return _currentFrame + 1;
		}
		
		public function get totalFrame():int{
			return _bitmapList.length;
		}
		
		public function get invokingFunction():String{return _invokingFunction;}
		
		public function set invokingFunction(value:String):void{_invokingFunction = value;}
		
		/**清理*/		
		public function dispose():void{
			clearInterval(_interval);
			_bitmap.parent.removeChild(_bitmap);
			_bitmap = null;
			_sourceList = new Vector.<String>();
			_bitmapList = new Vector.<BitmapData>();
			this.parent.removeChild(this);
		}
	}
}